using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using MeetingRoom.Data.Implementation;
using MeetingRoom.Domain;

namespace MeetingRoom.Data.Implementation
{
    public class SimpleMeetingRoomTypeQuery:
        AbstractDataAccessObject, 
        IGetMeetingRoomTypeById, 
        IGetMeetingRoomTypeBySize,
        IGetMeetingRoomTypes
    {
        #region IMeetingRoomTypeDao Members

        public MeetingRoom.Domain.MeetingRoomType GetMeetingRoomTypeById(int id)
        {
            string query = "select * from t_meetingroomtype" +
                " where meetingroomtype_id = @MeetingRoomTypeId";

            SqlParameter parameter = new SqlParameter("@MeetingRoomTypeId", id);
            SqlCommand command = new SqlCommand(query, (SqlConnection)this.connection);
            command.Parameters.Add(parameter);

            SqlDataReader dataReader = command.ExecuteReader();
            List<MeetingRoomType> meetingRoomTypes
                = MeetingRoomTypeDAOSupport.GetMeetingRoomTypes(dataReader);

            if (meetingRoomTypes != null && meetingRoomTypes.Count > 0)
            {
                return meetingRoomTypes[0];
            }

            return null;
        }

        public MeetingRoom.Domain.MeetingRoomType GetMeetingRoomTypeBySizeBySize(int size)
        {
            string query = "select * from t_meetingroomtype where size = @Size";

            SqlParameter sizeParameter = new SqlParameter("@Size", size);
            SqlCommand command = new SqlCommand(query, (SqlConnection)this.connection);
            command.Parameters.Add(sizeParameter);

            SqlDataReader dataReader = command.ExecuteReader();
            List<MeetingRoomType> meetingRoomTypes
                = MeetingRoomTypeDAOSupport.GetMeetingRoomTypes(dataReader);

            if (meetingRoomTypes != null && meetingRoomTypes.Count > 0)
            {
                return meetingRoomTypes[0];
            }

            return null;
        }

        #endregion

        #region IGetMeetingRoomTypes Members

        public MeetingRoom.Domain.MeetingRoomType[] GetMeetingRoomTypes(
            MeetingRoom.Domain.MeetingRoomType meetingRoomType)
        {
            StringBuilder buffer = new StringBuilder();
            buffer.Append("select * from t_meetingroomtype where ")
                .Append("size >= ")
                .Append(meetingRoomType.Size)
                .Append(" and ")
                .Append("projector")
                .Append("=\'")
                .Append(meetingRoomType.Projector)
                .Append("\'");

            SqlCommand command = new SqlCommand(buffer.ToString(), (SqlConnection) connection);
            command.Connection.Open();

            List<MeetingRoomType> meetingRoomTypes = null;
            SqlDataReader reader = null;

            try
            {
                reader = command.ExecuteReader();
                meetingRoomTypes = MeetingRoomTypeDAOSupport.GetMeetingRoomTypes(reader);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                reader.Close();
                command.Dispose();
            }

            return meetingRoomTypes.ToArray();
        }

        #endregion
    }
}
